package com.kingwood.algorithm.test;

import java.util.LinkedList;
import java.util.Scanner;

public class SlideWindow {

    /**
     * 求满足条件的最长子串的长度
     * 滑动窗口算法一般是双指针
     * https://fcqian.blog.csdn.net/article/details/127341878
     * @return
     */
    public static int maxLongOfSubStr(String str) {
        int ans = -1;
        int l = 0, r = 0;
        int len = str.length();
        boolean hasLetter = false;
        LinkedList<Integer> letterIdx = new LinkedList<>();

        while (r < len) {
            char c = str.charAt(r);
            if (isLetter(c)) {
                hasLetter = true;
                letterIdx.add(r);

                if (letterIdx.size() > 1) {
                    l = letterIdx.removeFirst() + 1;
                }

                if (r == l) {
                    r++;
                    continue;
                }
            }

            ans = Math.max(ans, r - l + 1);
            r++;
        }

        if (!hasLetter) return -1;
        return ans;
    }

    public static int maxLongOfSubStr2(String str) {
        int ans = -1;
        int l = 0, r = 0;
        int len = str.length();
        int letterCount = 0;
        boolean hasLetter = false;

        while (r < len) {
            char c = str.charAt(r);
            if (isLetter(c)) {
                letterCount++;
            }

            if (letterCount > 1) {
                while (l <= r && letterCount >= 2) {
                    if (isLetter(str.charAt(l))) {
                        letterCount--;
                    }
                    l++;
                }
            }

            if (letterCount == 1 && r > l) {
                // 结算答案
                ans = Math.max(ans, r - l + 1);
            }

            r++;
        }

        return ans;
    }

    public static boolean isLetter(char c) {
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            return true;
        }

        return false;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        System.out.println(maxLongOfSubStr(str));
        System.out.println(maxLongOfSubStr2(str));
    }
}
